#!/bin/bash

# 检查是否已经安装 Docker

function main(){
    if ! command -v docker >/dev/null 2>&1 ; then
    #运行安装脚本
        curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
    fi

    # 检查 Docker 是否已经启动
    if ! systemctl is-active docker >/dev/null 2>&1 ; then
        #启动 Docker
        systemctl start docker
        service docker start
    fi

    # 定义变量
    image_name="xzhouqd/qsign:core-1.1.5"
    container_name="qsign"
    port="7990"
    env_vars="-e BASE_PATH=/srv/qsign/qsign/txlib/8.9.68"

    # 启动 Docker 容器
    #判断是否有qsign容器，有则删除
    if [ -n "$(docker ps -a | grep $container_name)" ]; then
        echo "检测到qsign容器正在删除..."
        docker stop $container_name
        docker rm $container_name
    fi
    docker run -d --restart=always --name $container_name -p $port:8080 $env_vars $image_name

    #判断是否存在文件
    if [ ! -f "./config/config/bot.yaml" ]; then
        sed -i "s#sign_api_addr: #sign_api_addr: http://127.0.0.1:7990/sign#g" ./config/config/bot.yaml
    else
        sed -i "s#sign_api_addr: #sign_api_addr: http://127.0.0.1:7990/sign#g" ./config/default_config/bot.yaml
    fi

}
Yunzai_PATH=$1 # 从Go语言中传递变量给Shell脚本
if [ -d $Yunzai_PATH ]; then
    cd $Yunzai_PATH
fi
main